#include "shell.h"
#include "ringbuffer8.h"
#include "debug.h"
#include "board.h"

/* rb8参数结构 */
#define     RBBUFFER_SIZE           512
ringbuffer8_t serial_rb;
uint8_t serial_rx_buffer[RBBUFFER_SIZE];

/* shell参数结构体 */
Shell shell;
char shell_buffer[SHELL_BUFFER_SIZE];

static void usart_recv_handler(uint8_t data)       //将串口接收到的数据放入rb8
{
    if(!rb8_full(serial_rb))
    {
        rb8_put(serial_rb, data);
    }
}

static signed short _shell_write(char* data, unsigned short len)
{
    for(unsigned short i = 0;i < len;i++)
    {
        usart_sendbyte((uint8_t)data[i]);
    }
    return len;
}

void shell_task(void* param)
{
    usart_recv_register(usart_recv_handler);
    serial_rb = rb8_new(serial_rx_buffer, RBBUFFER_SIZE);
    shell.write = _shell_write;
    shellInit(&shell, shell_buffer, sizeof(shell_buffer));
    while(1)
    {
        uint8_t rx_data;
        if(!rb8_empty(serial_rb))        //如果rb8不为空
        {
            rb8_get(serial_rb, &rx_data);   //取出数据放入rx_data
            shellHandler(&shell, (char)rx_data);
        }
        vTaskDelay(10);
    }
}
